package com.reeygu.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reeygu.item.mapper.SeckillOrderMapper;
import com.reeygu.item.pojo.Order;
import com.reeygu.item.pojo.SeckillOrder;
import com.reeygu.item.pojo.User;
import com.reeygu.item.service.GoodsService;
import com.reeygu.item.service.OrderService;
import com.reeygu.item.service.SeckillOrderService;
import com.reeygu.item.vo.GoodsVo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author a1304
* @description 针对表【t_seckill_order】的数据库操作Service实现
* @createDate 2023-08-05 00:21:36
*/
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder>
implements SeckillOrderService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Order generatorKillAndOrder(GoodsVo goodsVo, User user) {
        Order order = Order.builder()
                .userId(user.getId())
                .goodsId(goodsVo.getId())
                .deliveryAddrId(1L)
                .goodsName(goodsVo.getGoodsName())
                .goodsCount(goodsVo.getStockCount())
                .goodsPrice(goodsVo.getGoodsPrice())
                .orderChannel(1)
                .creatDate(new Date())
                .status(0)
                .build();
        orderService.save(order);
        //生成秒杀订单，同时在redis也生成一份
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setUserId(user.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setGoodsId(goodsVo.getId());
        this.save(seckillOrder);
        redisTemplate.opsForValue().set("order:" + user.getId() + ":" + goodsVo.getId(), seckillOrder);
        return order;
    }
}
